﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;


public class UGUIEventListener : EventTrigger
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void PointerDelegate(GameObject go,PointerEventData data);
    public VoidDelegate onClick;
    public PointerDelegate onDown;
    public PointerDelegate onEnter;
    public PointerDelegate onExit;
    public PointerDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onUpdateSelect;
    public PointerDelegate onDragBegin;
    public PointerDelegate onDrag;
    public PointerDelegate onDrop;
    public PointerDelegate onDragEnd;

    public void AddClickAction(VoidDelegate clickAction)
    {
        onClick += clickAction;
    }

    public void RemoveClickAction(VoidDelegate clickAction)
    {
        if (clickAction != null) onClick -= clickAction;
    }

    public void AddDragAction(PointerDelegate onDragAction)
    {
        onDrag += onDragAction;
    }

    public void RemoveDragAction(PointerDelegate onDragAction)
    {
        if (onDrag != null)
        {
            onDrag -= onDragAction;
        }
    }

    public void AddEndDragAction(PointerDelegate onDragEndAction)
    {
        onDragEnd += onDragEndAction;
    }

    public void RemoveEndDragAction(PointerDelegate onDragEndAction)
    {
        if (onDragEnd != null)
        {
            onDragEnd -= onDragEndAction;
        }
    }

 

    static public UGUIEventListener Get(GameObject go)
    {
        UGUIEventListener listener = go.GetComponent<UGUIEventListener>();
        if (listener == null) listener = go.AddComponent<UGUIEventListener>();
        return listener;
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(gameObject,eventData);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(gameObject, eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject, eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(gameObject, eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }

    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject, eventData);
    }

    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject, eventData);
    }

    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onDragBegin != null) onDragBegin(gameObject, eventData);
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onDragEnd != null) onDragEnd(gameObject, eventData);
    }

    public delegate void MouseDownDelegate();
    public MouseDownDelegate onMouseDown;
    public void OnMouseDown()
    {
        if (onMouseDown != null) onMouseDown();
    }
    public delegate void OnTriggerEnterDelegate();
    public OnTriggerEnterDelegate triggerEnter;
    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 19)
            triggerEnter();
    }
}
